package org.cocos2dx.javascript.Mob;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

public class NetworkUtil
{
    private static String TAG = "MicroMsg.NetworkUtil";

    // 微信默认用GET
    public static void httpGet(Handler handler, String url, int msgTag) {
        HttpsThread httpsThread = new HttpsThread(handler, url, msgTag, "GET");
        httpsThread.start();
    }

    // FYHD默认用POST
    public static void httpPost(Handler handler, String url, int msgTag)
    {
        HttpsThread httpsThread = new HttpsThread(handler, url, msgTag, "POST");
        httpsThread.start();
    }

    public static void syncHttpPost(Handler handler, String httpsUrl, int msgTag)  {
        try{
            URL url = new URL(httpsUrl);
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpsConn = (HttpsURLConnection) urlConnection;
            //HttpsURLConnection httpsConn = (HttpsURLConnection) urlConnection;
            httpsConn.setAllowUserInteraction(false);
            httpsConn.setInstanceFollowRedirects(false);
            httpsConn.setRequestMethod("POST");
            httpsConn.connect();

            int resCode = httpsConn.getResponseCode();

            Log.e(TAG, httpsUrl);

            if (resCode == HttpURLConnection.HTTP_OK) {
                if (handler == null) return;

                InputStream in;
                String httpResult = null;

                in = httpsConn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        in, "UTF-8"), 8); // "iso-8859-1"
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                in.close();
                httpResult = sb.toString();
                Log.e(TAG, httpResult);
                Log.e(TAG, "" +msgTag);
                Message msg = Message.obtain();
                msg.what = msgTag;
                msg.obj = httpResult;
                handler.sendMessage(msg);
            }
        }
        catch (IOException e){

        }
    }

    static class HttpsThread extends Thread {
        private Handler handler;
        private String httpsUrl;
        private String method;
        private int msgTag;

        public HttpsThread(Handler handler, String url, int msgTag, String method) {
            this.handler = handler;
            this.httpsUrl = url;
            this.msgTag = msgTag;
            this.method = method;
        }

        @Override
        public void run() {
            int resCode;
            InputStream in;
            String httpResult = null;
            try {
                URL url = new URL(httpsUrl);
                URLConnection urlConnection = url.openConnection();
                HttpURLConnection httpsConn = (HttpURLConnection) urlConnection;
                //HttpsURLConnection httpsConn = (HttpsURLConnection) urlConnection;
                httpsConn.setAllowUserInteraction(false);
                httpsConn.setInstanceFollowRedirects(false);
                httpsConn.setRequestMethod(method);
                httpsConn.connect();
                resCode = httpsConn.getResponseCode();

                Log.e(TAG, httpsUrl);

                if (resCode == HttpURLConnection.HTTP_OK) {
                    if (handler == null) return;

                    in = httpsConn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            in, "UTF-8"), 8); // "iso-8859-1"
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                    in.close();
                    httpResult = sb.toString();
                    Log.e(TAG, httpResult);
                    Log.e(TAG, "" +msgTag);
                    Message msg = Message.obtain();
                    msg.what = msgTag;
                    msg.obj = httpResult;
                    handler.sendMessage(msg);
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    }
}
